package com.hellobike.haxing.modules.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.product.entity.PromotionFlash;
import com.hellobike.haxing.modules.product.service.IPromotionFlashService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 /**
 * @Description: 限时秒杀
 * @Author: haxing-boot
 * @Date:   2020-01-31
 * @Version: V1.0
 */
@RestController
@RequestMapping("/product/promotionFlash")
@Slf4j
public class PromotionFlashController extends BaseController<PromotionFlash, IPromotionFlashService> {
	@Autowired
	private IPromotionFlashService promotionFlashService;
	
	/**
	 * 分页列表查询
	 *
	 * @param promotionFlash
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "限时秒杀-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PromotionFlash promotionFlash,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
//		QueryWrapper<PromotionFlash> queryWrapper = QueryGenerator.initQueryWrapper(promotionFlash, req.getParameterMap());
		QueryWrapper<PromotionFlash> queryWrapper = new QueryWrapper<>(promotionFlash);
		queryWrapper.orderByAsc("sort_index");
		Page<PromotionFlash> page = new Page<PromotionFlash>(pageNo, pageSize);
		IPage<PromotionFlash> pageList = promotionFlashService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param promotionFlash
	 * @return
	 */
	@AutoLog(value = "限时秒杀-添加",operateType=2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody PromotionFlash promotionFlash) {
		return promotionFlashService.savePromotionFlash(promotionFlash);
	}
	
	/**
	 *  编辑
	 *
	 * @param promotionFlash
	 * @return
	 */
	@AutoLog(value = "限时秒杀-编辑",operateType=3)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PromotionFlash promotionFlash) {
		return promotionFlashService.updatePromotionFlash(promotionFlash);
	}

	 @GetMapping("/includeProductIds")
	 public Result<?> getIncludeProductIds(@RequestParam(name = "promotionId", required = true) Long promotionId) {
		 QueryWrapper<PromotionFlash> queryWrapper = new QueryWrapper<>();
		 queryWrapper.lambda().eq(PromotionFlash::getPromotionId, promotionId).select(PromotionFlash::getProductId);
		 List<Long> productIdList = promotionFlashService.listObjs(queryWrapper, productId -> (Long) productId);
		 return Result.ok(productIdList);
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "限时秒杀-删除",operateType=4)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
		promotionFlashService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "限时秒杀-批量删除",operateType=4)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.promotionFlashService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) Long id) {
		PromotionFlash promotionFlash = promotionFlashService.getPromotionFlashDetail(id);
		if(promotionFlash==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(promotionFlash);
	}

	 /**
	  * 秒杀商品上下架
	  * @param id
	  * @param status
	  * @return
	  */
	 @GetMapping(value = "/changeStatus")
	 public Result<?> changeOnlineStatus(@RequestParam(name = "id", required = true) Long id, @RequestParam(name = "status", required = true) Integer status) {
		 return promotionFlashService.changeStatus(id, status);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param promotionFlash
    */
    @AutoLog(value = "限时秒杀-导出excel",operateType=6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PromotionFlash promotionFlash) {
        return super.exportXls(request, promotionFlash, PromotionFlash.class, "限时秒杀");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @AutoLog(value = "限时秒杀-导入excel",operateType=5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PromotionFlash.class);
    }

}
